package com.roncoo.eshop.inventory.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.roncoo.eshop.inventory.dao.RedisDAO;
import com.roncoo.eshop.inventory.mapper.ProductInventoryMapper;
import com.roncoo.eshop.inventory.model.ProductInventory;
import com.roncoo.eshop.inventory.service.ProductInventoryService;

/**
 * Copyright: Copyright (c) 2019 Hu-Cheng
 * 
 * @ClassName: ProductInventoryServiceImpl.java
 * @Description: 商品库存业务实现类
 *
 * @version: v1.0.0
 * @author: hucheng
 * @date: 2019年9月19日 下午2:54:01
 *
 */
@Service
public class ProductInventoryServiceImpl implements ProductInventoryService {

	@Autowired
	private RedisDAO redisDAO;

	@Autowired
	private ProductInventoryMapper productInventoryMapper;

	@Override
	public void removeProductInventoryCache(ProductInventory productInventory) {
		String key = "product:inventory:" + productInventory.getProductId();
		redisDAO.del(key);
	}

	@Override
	public void updateProductInventory(ProductInventory productInventory) {
		productInventoryMapper.updateProductInventory(productInventory);
	}

	@Override
	public ProductInventory findProductInventory(Integer productId) {
		return productInventoryMapper.findProductInventory(productId);
	}

	@Override
	public void setProductInventoryCache(ProductInventory productInventory) {
		String key = "product:inventory:" + productInventory.getProductId();
		redisDAO.set(key, String.valueOf(productInventory.getInventoryCnt()));
	}

	@Override
	public ProductInventory getProductInventoryCache(Integer productId) {
		String key = "product:inventory:" + productId;
		String result = redisDAO.get(key);

		Long inventoryCut = 0L;

		if (result != null && "" != result) {
			inventoryCut = Long.valueOf(result);
			return new ProductInventory(productId, inventoryCut);
		}

		return null;
	}

}
